package com.sc.nft.controller;

import com.sc.nft.annotation.UserLogin;
import com.sc.nft.dao.EquityPropsDao;
import com.sc.nft.dto.IdRequest;
import com.sc.nft.entity.EquityProps;
import com.sc.nft.entity.WxMiniUserInfo;
import com.sc.nft.enums.UserEquityPropsGetTypeEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.feign.WxMiniRedeemCardClient;
import com.sc.nft.feign.WxMiniUserRedeemCardClient;
import com.sc.nft.pool.UserPool;
import com.sc.nft.req.redeemcard.UserRedeemCardPageReq;
import com.sc.nft.req.redeemcard.UserRedeemCardReceiveReq;
import com.sc.nft.req.redeemcard.UserRedeemCardRecordReq;
import com.sc.nft.service.UserEquityPropsService;
import com.sc.nft.service.WxMiniUserRedeemService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import com.sc.nft.vo.redeemcard.RedeemCardDetailsVO;
import com.sc.nft.vo.redeemcard.UserRedeemCardDetailsVO;
import com.sc.nft.vo.redeemcard.UserRedeemCardPageVO;
import com.sc.nft.vo.redeemcard.UserRedeemCardRecordsPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/ipMall/wxMiniMall/user/redeemCard")
@Api(tags = "小程序用户端用户兑换卡")
public class WxMiniUserRedeemCardController {


    private final WxMiniUserRedeemService wxMiniUserRedeemService;
    private final WxMiniRedeemCardClient wxMiniRedeemCardClient;

    /**
     * 用户兑换卡兑换
     * @param request
     * @return
     */
    @PostMapping(value = "/receive", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "兑换兑换卡")
    @UserLogin
    public Result receive(@Valid @RequestBody UserRedeemCardReceiveReq request) throws GlobalException {
        WxMiniUserInfo wxUser = UserPool.getWxMiniUser();
        request.setMiniUserId(wxUser.getId());
        request.setUserId(wxUser.getUserId());
        wxMiniUserRedeemService.receive(request);
        return Result.success();
    }

    /**
     * 用户兑换卡列表
     * @param request
     * @return
     */
    @PostMapping(value = "/userPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户兑换卡列表")
    @UserLogin
    public PageResult<UserRedeemCardPageVO> userPage(@Valid @RequestBody UserRedeemCardPageReq request) {
        WxMiniUserInfo wxUser = UserPool.getWxMiniUser();
        request.setMiniUserId(wxUser.getId());
        return wxMiniUserRedeemService.userPage(request);
    }



    /**
     * 用户兑换记录
     * @return
     */
    @PostMapping(value = "/records", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户兑换记录")
    @UserLogin
    public PageResult<UserRedeemCardRecordsPageVO> records(@RequestBody UserRedeemCardRecordReq req) {
        WxMiniUserInfo wxUser = UserPool.getWxMiniUser();
        req.setMiniUserId(wxUser.getId());
        return wxMiniUserRedeemService.records(req);
    }


    /**
     * 用户兑换卡详情
     * @param request
     * @return
     */
    @PostMapping(value = "/details", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户兑换卡详情")
    @UserLogin
    public Result<UserRedeemCardDetailsVO> details(@RequestBody IdRequest request) {
        return wxMiniUserRedeemService.details(request);
    }


    /**
     * 兑换卡使用规则查询
     * @return
     */
    @PostMapping(value = "/ruleGet", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "兑换卡使用规则查看")
    public Result<String> ruleGet() {
        return wxMiniRedeemCardClient.ruleGet();
    }



}